<template>
  <div class="component-marketing-distribution-item recruit-set">
    <el-form
      ref="dataFormRef"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
    >
      <div class="form-item">
        <div class="title">
          {{ $t("recruit.recruitPop") }}
        </div>
        <div class="form-item-content">
          <el-form-item
            :label="$t('recruit.popularizeSwitch')+':'"
            prop="state"
          >
            <el-radio-group v-model="dataForm.state">
              <el-radio :label="0">
                {{ $t("seckill.close") }}
              </el-radio>
              <el-radio :label="1">
                {{ $t("seckill.open") }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item :label="$t('recruit.coverImage')+':'">
            <img-upload v-model="dataForm.pic" />
          </el-form-item>

          <el-form-item
            :label="$t('recruit.popularizeTitle')+':'"
            prop="title"
          >
            <el-input
              v-model="dataForm.title"
              style="width: 900px"
              maxlength="60"
              show-word-limit
            />
          </el-form-item>

          <el-form-item
            :label="$t('recruit.popularizeContent')+':'"
            prop="content"
          >
            <tiny-mce
              ref="tinyMceRef"
              :key="tinymceFlag"
              v-model="dataForm.content"
              :tinymce-height="900"
            />
          </el-form-item>
          <el-form-item>
            <template #label>
              <span
                class="item"
                style="visibility: hidden;"
              >
                {{ $t('recruit.popularizeContent')+'*:' }}
              </span>
            </template>
            <div
              class="default-btn primary-btn"
              style="display: inline-block;"
              @click="onSubmit()"
            >
              {{ $t('distribution.save') }}
            </div>
          </el-form-item>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import TinyMce from '@/components/tiny-mce/index.vue'

const validateContent = (rule, value, callback) => {
  if (isHtmlNull(value)) {
    callback(new Error($t('product.content')))
  } else {
    callback()
  }
}

const dataRule = {
  title: [
    { required: true, message: $t('recruit.tipTitleTip1'), trigger: 'brue' },
    { max: 255, message: $t('recruit.limitLangTip2'), trigger: 'brue' }
  ],
  content: [
    { required: true, message: $t('product.content'), trigger: 'brue' },
    { validator: validateContent, trigger: 'blur' }
  ]
}
onMounted(() => {
  getData()
})

const tinymceFlag = ref(new Date() + 1)
// keep-alive缓存的缘故 需调用钩子函数 设置key值不然编辑器无法使用
onActivated(() => {
  tinymceFlag.value = new Date() + 1
})

/**
 * 获取数据
 */
const getData = () => {
  http({
    url: http.adornUrl('/platform/distributionConfig/recruit_info'),
    method: 'get'
  })
    .then(({ data }) => {
      if (data) {
        setDate(data)
      }
    })
}

const dataForm = ref({
  pic: '',
  title: '',
  content: ''
})
/**
 * 设置数据
 * @param data
 */
const setDate = (data) => {
  dataForm.value = data
}

const dataFormRef = ref(null)
/**
 * 提交表单
 */
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      const param = dataForm.value
      http({
        url: http.adornUrl('/platform/distributionConfig/recruit'),
        method: 'post',
        data: http.adornData(param)
      })
        .then(() => {
          ElMessage({
            message: $t('publics.operation'),
            type: 'success',
            duration: 1500
          })
        })
    }
  })
}
</script>
